package unitTest;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

import sqlengine.database.Index;



public class IndexTest {
	// // only for test
	public static class Record extends sqlengine.database.impl.Record implements Serializable {
		private Map<String, String> record;
		private int recordID;

		public Map<String, String> getRecord() {
			return record;
		}

		public void setRecord(Map<String, String> record) {
			this.record = record;
		}

		public String getKey(String columnName) {
			return record.get(columnName);
		}

		public int getRecordID() {
			return recordID;
		}

		public void setRecordID(int recordID) {
			this.recordID = recordID;
		}

		public Record() {
		}
	}

	public static void main(String[] avg) {
		Vector<sqlengine.database.impl.Record> records = new Vector<sqlengine.database.impl.Record>();
		Record record;
		Map<String, String> map;
		int recordSize = 100;
		for (int i = 0; i < recordSize; i++) {
			record = new Record();
			map = new HashMap<String, String>();
			map.put("Age", Integer.toString(i));
			map.put("hight", Integer.toString(i % 10 + 170));
			record.setRecord(map);
			record.setRecordID(i);
			records.add(record);
		}
		// insert test
		System.out.println("insert test");
		Index indexAge = new Index("Emp","indexAge","Age", records);
		indexAge.viewIndex();
		Index indexHight = new Index("Emp","indexHight","hight", records);
		indexHight.viewIndex();
		// delete test
		// System.out.println("delete test");
		// // test for large data
		// // in order delete
		// for (int i = 0; i < recordSize / 2; i++) {
		// // System.out.println("delete " + i);
		// indexAge.deleteRecord(records.get(i));
		// }
		// for (int i = 0; i < recordSize - recordSize / 2; i++) {
		// records.get(i + recordSize / 2).setRecordID(i);
		// }
		// indexAge.viewIndex();
		// System.out.println("dump, restore test");
		// indexAge.dumpIndex();
		// Index indexAgeRestore = Index.restoreIndex("Emp", "Age");
		// indexAgeRestore.viewIndex();
		// // reverse order delete
		// for (int i = recordSize - 1; i >= 0; i--) {
		// System.out.println("delete " + i);
		// indexAge.deleteRecord(records.get(i));
		// indexAge.viewIndex();
		//
		// }
		// test for duplicate key
		// iterator
		// unique
		Iterator<Integer> itAge;
		System.out.println("Iterator Test for unique record ID");
		for (int i = -10; i < 300; i += 30) {
			itAge = indexAge.iterator(Integer.toString(i));
			if (itAge != null) {
				System.out.print("key: " + i + " Record ID: ");
				while (itAge.hasNext()) {
					System.out.print(itAge.next() + " ");
				}
				System.out.println();
			} else {
				System.out.println("can't find key: " + i);
			}
		}
		System.out.println();
		// duplicate
		Iterator<Integer> itHight;
		System.out.println("Iterator Test for duplicate record ID");
		for (int i = 162; i < 200; i += 10) {
			itHight = indexHight.iterator(Integer.toString(i));
			if (itHight != null) {
				System.out.print("key: " + i + " Record ID: ");
				while (itHight.hasNext()) {
					System.out.print(itHight.next() + " ");
				}
				System.out.println();
			} else {
				System.out.println("can't find key: " + i);
			}
		}
		// remove test
		Iterator<Integer> it1 = indexHight.iterator(Integer.toString(172));
		Iterator<Integer> it2 = indexHight.iterator(Integer.toString(172));
		Iterator<Integer> it3 = indexHight.iterator(Integer.toString(172));
		
		it1.next();
		it1.next();
		it3.next();
		it1.remove();
		it3 = indexHight.iterator(Integer.toString(172));
		while(it3.hasNext()){
			System.out.print(it3.next() + " ");
		}
		System.out.println();
		
		// test for b plus tree iterator
//		Iterator<RecordArray> it = indexHight.getBPlusTreeIterator();
//		while(it.hasNext()){
//			RecordArray ra = it.next();
//			for(int i = 0;i<ra.size();i++){
//				System.out.print(ra.get(i)+" ");
//			}
//		}
//		it2.remove();
		
		// in order delete
//		for (int i = 0; i < recordSize; i++) {
//			System.out.println("delete " + i);
//			indexHight.deleteRecord(records.get(i));
//			indexHight.viewIndex();
//		}
		// reverse order delete
		// for (int i = recordSize - 1; i >= 0; i--) {
		// System.out.println("delete " + i);
		// indexHight.deleteRecord(records.get(i));
		// indexHight.viewIndex();
		// }
	}
}
